package com.lms.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.neo4j.conversion.EndResult;
import org.springframework.stereotype.Service;

import com.lms.domain.NewProjects;
import com.lms.domain.Profile;
import com.lms.domain.User;
import com.lms.dto.UserDto;
import com.lms.repository.NewProjectRepository;
import com.lms.repository.ProfileRepository;
import com.lms.repository.ProjectRepository;
import com.lms.repository.RoleRepository;
import com.lms.repository.TrainingRepository;
import com.lms.repository.UserRepository;

@Service
public class UserService {

	@Autowired
	private UserRepository userRepository;
	
	@Autowired
	private RoleRepository roleRepository;
	
	@Autowired
	private ProjectRepository projectRepository;
	
	@Autowired
	private TrainingRepository trainingRepository;
	
	@Autowired
	private ProfileRepository profileRepository;
	
	
	
	public User create(User user) {
		User existingUser = userRepository.findByUsername(user.getUsername());
		
		if (existingUser != null) {
			throw new RuntimeException("Record already exists!");
		}
		System.out.println("create 1");
		user.getRole().setUser(user);
		user.getProject().setUser(user);
		user.getTraining().setUser(user);
		
		return userRepository.save(user);
	}
	
	public User read(User user) {
		return user;
	}
	
	public List<User> readAll() {
		List<User> users = new ArrayList<User>();
		
		EndResult<User> results = userRepository.findAll();
		for (User r: results) {
			System.out.println("r.getRole() "+r.getRole());
			System.out.println("r.getProject() "+r.getProject());
			System.out.println("r.getTraining() "+r.getTraining());
			
			users.add(r);
		}
		
		return users;
	}
	
	public User update(User user) {
		User existingUser = userRepository.findByUsername(user.getUsername());
		
		if (existingUser == null) {
			return null;
		}
		
		existingUser.setFirstName(user.getFirstName());
		existingUser.setLastName(user.getLastName());
		if(user.getRole() !=null && user.getRole().getRole() !=null )
		existingUser.getRole().setRole(user.getRole().getRole());
		if(user.getProject() !=null && user.getProject().getProject() !=null )
		existingUser.getProject().setProject(user.getProject().getProject());
		if(user.getTraining() !=null && user.getTraining().getTraining() !=null )
		existingUser.getTraining().setTraining(user.getTraining().getTraining());
		
		roleRepository.save(existingUser.getRole());
		projectRepository.save(existingUser.getProject());
		trainingRepository.save(existingUser.getTraining());
		
		return userRepository.save(existingUser);
	}
	
	public Boolean delete(User user) {
		User existingUser = userRepository.findByUsername(user.getUsername());
		
		if (existingUser == null) {
			return false;
		}
		
		userRepository.delete(existingUser);
		return true;
	}
	
	public Profile readProfile(Profile profile) {
		Profile newProfile = profileRepository.findByPanNo(profile.getPanNo());
		System.out.print("readProfile... "+profile.getPanNo());
		return newProfile;
		}
	
	public Profile createProfile(Profile profile) {
		//Profile existingUser = userRepository.findByUsername(profile.getUsername());
		
		/*if (existingUser != null) {
			throw new RuntimeException("Record already exists!");
		}
*/		
		//profile.getRole().setUser(profile);
		return profileRepository.save(profile);
	}
	
	public Profile updateProfile(Profile profile) {
		Profile existingProfile = profileRepository.findByPanNo(profile.getPanNo());
		
		if (existingProfile == null) {
		 return createProfile(profile);
		}
	
		
		existingProfile.setFirstName(profile.getFirstName());
		existingProfile.setLastName(profile.getLastName());
		existingProfile.setAddress(profile.getAddress());
		
		existingProfile.setPhone(profile.getPhone());
		existingProfile.setPanNo(profile.getPanNo());
		
		profileRepository.save(existingProfile);
		return profileRepository.save(existingProfile);
	}
	
	public User readEmployeeSearch(User user) {
		User existingUser = userRepository.findByUsername(user.getUsername());
		
		if (existingUser != null) {
			System.out.println("Search result..."+existingUser.getFirstName());
			return existingUser;
		}
		else
			System.out.println("No Such User");
		
		return null;
		
		
	}
	
	
}
